package com.dmwork.service.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmwork.service.customer.entity.Customer;
import com.dmwork.service.customer.entity.CustomerFollower;
import com.dmwork.service.customer.mapper.CustomerFollowerMapper;
import com.dmwork.service.customer.mapper.CustomerMapper;
import com.dmwork.service.customer.service.CustomerFollowerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户关注记录表 服务实现类
 * </p>
 *
 * @author 李天
 * @since 2021-03-10
 */
@Slf4j
@Service
@Transactional
public class CustomerFollowerServiceImpl extends ServiceImpl<CustomerFollowerMapper, CustomerFollower> implements CustomerFollowerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private CustomerFollowerMapper customerFollowerMapper;

    @Override
    public void follow(String customerId, String followerId, String followGroupId) {
        //更新关注数据表
        CustomerFollower customerFollower = new CustomerFollower();
        customerFollower.setCustomerId(customerId);
        customerFollower.setFollowerId(followerId);
        customerFollower.setFollowGroupId(followGroupId);
        baseMapper.insert(customerFollower);

        //更新登录用户的关注数+1
        Customer followCustomer = customerMapper.selectById(followerId);
        customerMapper.updateById(followCustomer);
        //被关注用户的粉丝数+1
        Customer customer = customerMapper.selectById(customerId);
        customerMapper.updateById(customer);
    }

    @Override
    public void unFollow(String customerId, String followerId) {
        QueryWrapper<CustomerFollower> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", customerId)
                .eq("follower_id", followerId);
        baseMapper.delete(queryWrapper);

        //更新登录用户的关注数
        Customer followCustomer = customerMapper.selectById(followerId);
        customerMapper.updateById(followCustomer);
        //被关注用户的粉丝数
        Customer customer = customerMapper.selectById(customerId);
        customerMapper.updateById(customer);
    }

    @Override
    public void updateFollowGroup(String customerId, String followerId, String followGroupId) {
        CustomerFollower customerFollower = new CustomerFollower();
        customerFollower.setFollowGroupId(followGroupId);
        QueryWrapper<CustomerFollower> queryWrapper = new QueryWrapper<CustomerFollower>().eq("customer_id", customerId).eq("follower_id", followerId);
        customerFollowerMapper.update(customerFollower, queryWrapper);
        log.info(customerFollower.toString());
    }

    @Override
    public void unFollowAll(String followerId, String followGroupId) {
        QueryWrapper<CustomerFollower> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("follower_id", followerId).eq("follow_group_id", followerId);
        baseMapper.delete(queryWrapper);
    }

    @Override
    public List<String> getFollowerIdList(String customerId) {
        return baseMapper.getFollowerIdList(customerId);
    }

    @Override
    public List<String> getFollowedCustomerIdList(String customerId) {
        return baseMapper.getFollowedCustomerIdList(customerId);
    }
}
